#!/bin/sh
# Copyright (C) 2024-2025 KoudaiPC Computer Technologies Co., Ltd.
# script name: huangdi-cleanup-system.sh
# author: david921518@qq.com

let COUNTER=0
WAN_IP=$(ubus call network.interface.wan status | grep "address" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
while [ "${WAN_IP}"x == x ]; do
  sleep 1
  let COUNTER=$COUNTER+1
  if [ $COUNTER -gt 29 ]; then
    echo "Can't get WAN IP address, reboot now" 2>&1 | tee /dev/kmsg
    while true; do reboot; sleep 9; done
  fi
  WAN_IP=$(ubus call network.interface.wan status | grep "address" | grep -oE '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
done

echo "[cleanup-system] WAN IP=[${WAN_IP}]" 2>&1 | tee /dev/kmsg
sleep 1
echo "[cleanup-system] PATH=[$PATH]" 2>&1 | tee /dev/kmsg
sleep 1

# 1. remove dropbear keys
rm -f /etc/dropbear/dropbear_ed25519_host_key
rm -f /etc/dropbear/dropbear_rsa_host_key

# 2. remove nginx _lan key/cert
rm -f /etc/nginx/conf.d/_lan.key
rm -f /etc/nginx/conf.d/_lan.crt

sync
sleep 1

echo "[cleanup-system] EXIT" 2>&1 | tee /dev/kmsg
sleep 1

# restart system
#poweroff

